package com.qf.game.servlet;

import com.qf.framwork.annotation.ResponseBody;
import com.qf.framwork.base.BaseServlet;
import com.qf.framwork.uploader.MultipartFile;
import com.qf.framwork.utils.PropertiesUtils;
import com.qf.game.entity.Player;
import com.qf.game.service.PlayerService;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.Date;
import java.util.UUID;

/**
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@WebServlet(urlPatterns = "/fileServlet")
public class FileServlet extends BaseServlet {

    private PlayerService playerService = new PlayerService();

    /**
     * 上传头像 - ajax
     * @return
     */
    @ResponseBody
    public String uploadHeader(MultipartFile file, HttpSession session) throws FileNotFoundException {
        //上传路径
        String path = PropertiesUtils.getProperties("uploader.img.path");
        File pathFile = new File(path);
        //判断该路径是否存在
        if (!pathFile.exists()) {
            //路径不存在，则创建路径
            pathFile.mkdirs();
        }
        //自动生成一个文件名称
        String fileName = UUID.randomUUID().toString();
        //将路径和文件名结合成一个file

        //进行拷贝
        try (
                //准备一个输出流
                OutputStream out = new FileOutputStream(new File(pathFile, fileName));
                //再获得上传的输入流
                InputStream in = file.getInputStream();
        ){
            //进行头像的拷贝
            IOUtils.copy(in, out);

            //将该头像的信息同步到用户的数据库中
            Player player = (Player) session.getAttribute("login_user");
            //设置当前用户的头像信息
            player.setHeader(fileName);
            player.setUpdateTime(new Date());
            //调用service层更新当前的用户信息
            int result = playerService.updatePlayer(player);
            if (result > 0) {
                //修改头像信息成功
                session.setAttribute("login_user", player);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "{\"imgName\":\"" + fileName + "\"}";
    }

    /**
     * 回显头像
     * @return
     */
    public void showImg(String imgName, HttpServletResponse response) {
        //上传路径
        String path = PropertiesUtils.getProperties("uploader.img.path");
        try (
                //输入流
                InputStream in = new FileInputStream(new File(path, imgName));
                //输出流
                ServletOutputStream out = response.getOutputStream();
        ) {
            //开始拷贝
            IOUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 上传音频
     */
    @ResponseBody
    public String uploadAudio(MultipartFile file){
        //上传路径
        String path = PropertiesUtils.getProperties("uploader.audio.path");
        File pathFile = new File(path);
        //判断该路径是否存在
        if (!pathFile.exists()) {
            //路径不存在，则创建路径
            pathFile.mkdirs();
        }
        //自动生成一个文件名称
        String fileName = UUID.randomUUID().toString();
        //将路径和文件名结合成一个file

        //进行拷贝
        try (
                //准备一个输出流
                OutputStream out = new FileOutputStream(new File(pathFile, fileName));
                //再获得上传的输入流
                InputStream in = file.getInputStream();
        ){
            //进行头像的拷贝
            IOUtils.copy(in, out);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "{\"audioName\":\"" + fileName + "\"}";
    }


    /**
     * 获取音频信息
     * @return
     */
    public void showAudio(String audioName, HttpServletResponse response) {
        //上传路径
        String path = PropertiesUtils.getProperties("uploader.audio.path");
        try (
                //输入流
                InputStream in = new FileInputStream(new File(path, audioName));
                //输出流
                ServletOutputStream out = response.getOutputStream();
        ) {
            //开始拷贝
            IOUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
